<?php

namespace Xuan\Controller;

class AreaController extends CommonController {

    public function lists() {
        $id = I('get.id', '0', 'int');
        $sql = "pid = " . $id . "";
        $pid = I('get.pid');
        if ($pid) {
            $sql = "pid = " . $pid . "";
        }
        $detail = M('area')->where("id = " . $id . "")->find();
        $keywords = trim(I('get.keywords'));
        if ($keywords) {
            $sql .= " AND name like '%" . $keywords . "%'";
        }
        $count = M('area')->where($sql)->count();    //计算总数
        $Page = new \Think\Page($count, C("pagenum"));
        $lists = M('area')->where($sql)->limit($Page->firstRow . ',' . $Page->listRows)->order('ord ASC')->select();
        $page = $Page->show();
        $this->assign("page", $page);
        $this->assign("lists", $lists);
        $this->assign("keywords", $keywords);
        $this->assign("detail", $detail);
        $this->assign("id", $id);
        $this->display();
    }

    public function detail() {
        $id = I('get.id', '0', 'int');
        if ($id == 0) {
            $detail['is_check'] = 1;
            $id = I('get.add_id', '0', 'int');
        } else {
            $detail = M("area")->where("id =" . $id . "")->find();
        }
        $this->assign("detail", $detail);
        $this->assign("id", $id);
        $this->display();
    }

    public function detail_post() {
        $id = I('post.id', '', 'int');
        $data['pid'] = I('post.pid', '0', 'int');
        $names = I('post.name');
        $data['cname'] = I('post.cname');
        $data['is_check'] = I('post.is_check', '0', 'int');
        $info = M("area")->where("id = " . $data['pid'] . "")->find();
        if ($info) {
            $data['level'] = $info['level'] + 1;
        }
        if ($id > 0) {
            $data['name'] = $names;
            $data['ord'] = I('post.ord', '30', 'int');
            M("area")->where("id = " . $id . "")->save($data);
            $info = M("area")->where("id = " . $id . "")->find();
        } else {
            $names = array_unique(array_filter($names));
            foreach ($names as $k => $v) {
                if ($v) {
                    $data['name'] = $v;
                    $data['ord'] = I('post.ord', '30', 'int') + $k;
                    M("area")->add($data);
                }
            }
            $info['pid'] = I("post.addid", '0', 'int');
        }
        if ($id > 0) {
            $this->success('修改成功！', session('QUERY_STRING'));
        } else {
            $this->success('添加成功！', U('Area/lists', array("id" => $data['pid'])));
        }
        clearTempFile();
    }
    public function circle() {
        
        $keyword = trim(I('get.keyword'));
        $sql = "level = 2";
        if ($keyword != '') {
            $sql .= " AND name like '%" . $keyword . "%'";
        }
           $searchId = I("get.searchId",0,'int');
           if($searchId != 0){
                $searchIdImlode =  getAreaSubIds($searchId);
               if($searchIdImlode){
                   $searchIdImlode = $searchIdImlode.$searchId;
               }else{
                   $searchIdImlode = $searchId;
               }
               $sql .= " AND pid in (".$searchIdImlode.")";
           }
           
        $count = M('area')->where($sql)->count();    //计算总数
        $Page = new \Think\Page($count,C("pagenum"));
        $lists = M('area')->where($sql)->limit($Page->firstRow . ',' . $Page->listRows)->order('ord ASC')->select();
        $page = $Page->show();
        $this->assign("page", $page);
        $this->assign("lists", $lists);
        $this->assign("keyword", $keyword);
        $this->assign("id", $searchId);
        $this->display();
    }

    public function circle_detail() {
        $id = I('get.id', '0', 'int');
        $detail = M('area')->where("id =" . $id . "")->find();
        if ($id == 0) {
            $detail['is_check'] = 1;
        }
        $this->assign("detail", $detail);
        $this->display();
    }

    public function circle_detail_post() {
        
        $id = I('post.id', '', 'int');
        $data['pid'] = I('post.pid', '0', 'int');
        $names = I('post.name');
        $info = M("area")->where("id = ".$data['pid']."")->find();
        if($info){
        $data['level'] = $info['level']+1;
        }
        $data['is_check'] = I('post.is_check', '0', 'int');
        if ($id > 0) {
            $data['name'] = $names;
            $data['ord'] = I('post.ord', '30', 'int');
            M("area")->where("id = " . $id . "")->save($data);
            $info = M("area")->where("id = " . $id . "")->find();
        } else {
            foreach ($names as $k=>$v) {
                if ($v) {
                    $data['ord'] = I('post.ord', '30', 'int')+$k;
                    $data['name'] = $v;
                    M("area")->add($data);
                }
            }
        }
        if ($id > 0) {
            $this->success('修改成功！', session('QUERY_STRING'));
        } else {
            $this->success('添加成功！', U('Area/circle'));
        }
        clearTempFile();
    }

}

?>
